<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="visible" fullscreen center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="90px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="单据编号" prop="billId">
            <el-input v-model="dataForm.billId" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="订单编号">
            <el-input readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="单据日期">
            <el-input v-model="dataForm.createTime" readonly></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="所属制造商">
            <el-input v-model="dataForm.supplyName" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="入库类型">
            <el-input value="采购入库" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="接收仓位">
            <el-input v-model="dataForm.inStockName" readonly></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="制单人">
            <el-input v-model="dataForm.createUserName" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="入库审核人">
            <el-input v-model="dataForm.auditUserName" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="运单号">
            <el-input readonly></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="memo">
            <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="dataForm.memo" readonly></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 采购商品列表 -->
      <el-table :data="dataForm.detailList" border v-loading="dataListLoading" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column key="2" prop="code" label="商品代码" width="170">
        <template slot-scope="scope">
          <i class="el-icon-circle-plus-outline" style="color: #409eff;font-size: 18px;" @click="addNewGoods(scope.row,scope.$index)"></i>{{scope.row.code}}
        </template>
      </el-table-column>
      <el-table-column key="3" prop="name" label="商品名称" width="170"></el-table-column>
      <el-table-column key="4" prop="spec" label="型号" v-if="dataForm.ctgId != 3 && dataForm.ctgId != 4"></el-table-column>
      <el-table-column key="9" prop="mirrorBall" label="球镜" width="50" v-if="[3,4].includes(dataForm.ctgId)"></el-table-column>
      <el-table-column key="10" prop="columnMirror" label="柱镜" width="50" v-if="[3,4].includes(dataForm.ctgId)"></el-table-column>
      <el-table-column key="13" prop="primaryCapacity" label="主容量" v-if="dataForm.ctgId == 5"></el-table-column>
      <el-table-column key="14" prop="secondaryCapacity" label="次容量" v-if="dataForm.ctgId == 5"></el-table-column>
      <el-table-column key="15" prop="manufacturerName" label="生产厂家"></el-table-column>
      <el-table-column key="16" prop="productionDate" label="生产日期"></el-table-column>
      <el-table-column key="17" prop="productionDate" label="效期" ></el-table-column>
      <el-table-column key="18" prop="batch" label="批号" width="120"></el-table-column>
      <el-table-column key="19" prop="registerNum" label="注册证号"></el-table-column>
      <el-table-column key="20" prop="goodsBarcode" label="商品条码" width="251" v-if="[4,5].includes(dataForm.ctgId)"></el-table-column>
      <el-table-column key="21" prop="goodsBarcode" width="251" label="商品条码" v-if="dataForm.ctgId != 4 && dataForm.ctgId != 5"></el-table-column>
      <el-table-column key="22" prop="quantity" label="数量" width="80"></el-table-column>
      <el-table-column key="23" prop="acceptanceQuantity" label="验收数量" width="80"></el-table-column>
      <el-table-column key="24" prop="acceptanceMemo" label="验收结果" width="80"></el-table-column>
    </el-table>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
    </span>
    <!-- 弹窗，查询选择制造商 -->
    <supply-search v-if="supplySearchVisible" ref="supplySearch" @refreshData="checkSupply"></supply-search>
  </el-dialog>
</template>

<script>
  import {filterParams} from '@/util/tools'
  import { receiveGoodsGetObj, updateAcceptanceById } from '@/api/eims/purchase/receive-goods'
  import { fetchCtgList } from '@/api/eims/ctg'
  import { getSupplyObj } from '@/api/eims/supply'
  import { fetchStockList, getStockInfo,getStockList,getStockByDeptTypes } from '@/api/eims/stock'
  import SupplySearch from '@/components/business/SearchSupply'
  import { mapGetters } from 'vuex'
  export default {
    data() {
      return {
				ischeck: false,
        visible: false,
        title: '',
        dataForm: {
          id: 0,
          inStockId: '',
          outStockId: '',
          supplyId: '',
          supplyName: '',
          ctgId: '',
          memo: '',
          inveStatus: '0',
          detailList: []
        },
        ctgOptions: [],
        stockOptions: [],
        dataRule: {
          inStockId: [{ required: true, message: '入库仓库id不能为空', trigger: 'blur' }],
          outStockId: [{ required: true, message: '出库仓库id不能为空', trigger: 'blur' }],
          supplyName: [{ required: true, message: '制造商不能为空', trigger: 'blur' }],
          ctgId: [{ required: true, message: '商品类别id不能为空', trigger: 'blur' }]
        },
        supplySearchVisible: false,
        singleSearchVisible: false,
        bivariateVisible: false,
        dataList: [],
        dataListLoading: false,
        selectList: [],
        orderVisible: false,
      }
    },
    computed: {
        ...mapGetters(['bizData','permissions','userInfo'])
    },
    components: {
      SupplySearch
    },
    methods: {
      init(data) {
        this.dataForm.id = data.id || 0
        this.type = data.type
        if(this.type == '4'){
          this.title = '隐形采购验收更新'
        }else{
          this.title = '采购验收更新'
        }
        this.visible = true
        this.getStockByDeptId()
        this.getStockList()
        this.getGoodsCtgList()
        this.dataList = []
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            receiveGoodsGetObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
                this.dataList = this.dataForm.detailList
                if(this.dataList.length){
                  this.dataList.forEach(item => {
                    //item = Object.assign(item,item.goods)
                    item.name = item.goodsName
                    item.code = item.goodsCode
                    if(!item.acceptanceQuantity){
                      this.$set(item,'acceptanceQuantity',item.quantity)
                    }
                    if(item.invalidTime) {
                      item.invalidTimeStr = item.invalidTime.substring(0,11)
                    }
                  })
                }
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      //  打开采购订单选择
      selectOrderDialog() {
        this.orderVisible = true
        this.$nextTick(() => {
          this.$refs.orderDialog.init()
        })
      },
      handleSelectionChange(val) {
        this.selectList = val
      },
      quantityChangeHangle(val){

      },
      // 获取商品类别列表
      getGoodsCtgList() {
        fetchCtgList().then(response => {
          if(response.data.code == '0'){
            let resultData = response.data.data.records
            let list = []
            resultData.forEach(item => {
              if(this.type == 4 || this.type == 5){
                if(item.id == 4 || item.id == 5){
                  list.push({value:item.id,name:item.name})
                }
              }else{
                if(item.id != 4 && item.id != 5){
                  list.push({value:item.id,name:item.name})
                }
              }
            })
            this.ctgOptions = list
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 打开选择制造商弹窗
      selectSupplyDialog() {
        if(this.dataForm.ctgId){
          let data = {ctgId: this.dataForm.ctgId}
          this.supplySearchVisible = true
          this.$nextTick(() => {
            this.$refs.supplySearch.init(data)
          })
        }else {
          this.$message.warning('请选择采购类别')
        }

      },
      ctgChangeHandle(val){
        this.dataForm.supplyId = ''
        this.dataForm.supplyName = ''
        this.dataList = []
        this.dataForm.detailList = []
        this.ctgOptions.forEach(item => {
          if(item.value == val){
            this.dataForm.ctgName = item.name
          }
        })
      },
      quantityChangeHandle(val){
        this.dataList.forEach((item,index) => {
          this.$set(this.dataForm.detailList[index],'quantity',item.quantity)
        })
      },

      // 表单提交
      dataFormSubmit() {
				this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            for(let i = 0; i < this.dataList.length;i++){
              if(!this.dataList[i].acceptanceMemo){
                this.$message.error('请填写验收结果')
								this.ischeck = false
                return false
              }
            }
            this.dataForm.detailList = [...this.dataList]
            this.dataForm.detailList.forEach(item => {
              item = filterParams(item)
            })
            if (this.dataForm.id) {
              updateAcceptanceById(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  (this.dataForm.acceptanceStatus == "1") ? this.$message.success('验收并审核成功') : this.$message.success('验收修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
							  this.ischeck = false
              }).catch(() => {
								this.ischeck = false
							})
            }
          }else{
						this.ischeck = false
					}
        })
      },

      // 获取仓位列表
      getStockList() {
        var data ={
          stockTypes : [1]
        }
        getStockByDeptTypes(data).then(response => {
          if(response.data.code == '0'){
            this.stockOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      getStockByDeptId(){
        getStockInfo(this.userInfo.lessDeptId).then(response => {
          if(response.data.code == '0'){
            this.dataForm.inStockId = response.data.data[0].id
          }else{
            this.$message.error(response.data.msg)
          }
        })
      }
    }
  }
</script>
